<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

$no_menu = 1;

class SubscribePage {

	private $alerter;

	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		if (!$_SESSION['user_id'])
			errorpage(6);
		check_access("READ");
	}

	/**
	 * appropriate page action interpretations
	 */
	function page_inc_execute_action($action) {
		switch($action) {
			default:
				$alerter->add_alert("unknown action: $action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {

		if ($_POST['submitted']) { // execute subscription
		
			if ($page->id) { // item
			
				if ($_POST['type'] == "UNSUBSCRIBE")
					$sql = db_query_delete("SUBSCRIBE_ITEM", "user_id = {$_SESSION['user_id']} AND item_id = {$page->id}");
				else {
					$sql = db_query_select(array(array('*')), array("SUBSCRIBE_ITEM"), "user_id = {$_SESSION['user_id']} AND item_id = {$page->id}");
					$sqlresult = db_do_query($page, $sql);
					if (db_num_rows($sqlresult)) {
						$subscription = db_fetch_array($sqlresult);
						$sql = db_query_update(array('subscription_id'), array($_POST['type']), "SUBSCRIBE_ITEM", "user_id = {$_SESSION['user_id']} AND item_id = {$page->id}");
					} else 
						$sql = db_query_insert(array('item_id', 'user_id', 'subscription_id'), array($page->id, $_SESSION['user_id'], $_POST['type']), "SUBSCRIBE_ITEM");
				}
				
				db_do_query($page, $sql);
				endpage("ok, abonnement gewijzigd");

				
			} else { // directory
			
				$dir_id = $page->directory_tree[$page->dir_depth-1]['dir_id'];

				if ($_POST['type'] == "UNSUBSCRIBE")
					$sql = db_query_delete("SUBSCRIBE_DIR", "user_id = {$_SESSION['user_id']} AND dir_id = $dir_id");
				else {
					$sql = db_query_select(array(array('*')), array("SUBSCRIBE_DIR"), "user_id = {$_SESSION['user_id']} AND dir_id = $dir_id");
					$sqlresult = db_do_query($page, $sql);
					if (db_num_rows($sqlresult)) {
						$subscription = db_fetch_array($sqlresult);
						$sql = db_query_update(array('subscription_id'), array($_POST['type']), "SUBSCRIBE_DIR", "user_id = {$_SESSION['user_id']} AND dir_id = $dir_id");
					} else 
						$sql = db_query_insert(array('dir_id', 'user_id', 'subscription_id'), array($dir_id, $_SESSION['user_id'], $_POST['type']), "SUBSCRIBE_DIR");
				}
				
				db_do_query($page, $sql);
				endpage("OK, abonnement gewijzigd");
			
			}
		
		} else {
		
			if ($page->id) { // item subscription
			
				$page->content['title'] = "Abonnement op item #{$page->id} wijzigen";
			
				$inputs = array();
				
				$sql = db_query_select(array(array('*')), array("SUBSCRIPTION_TYPE"), 1);
				$sqlresult = db_do_query($page, $sql);
				$keys = array();
				$vals = array();
				while ($r = db_fetch_array($sqlresult)) {
					$keys[] = $r['subscription_id'];
					$vals[] = $r['name'];
				}
				
				$sql = db_query_select(array(array('*')), array("SUBSCRIBE_ITEM"), "user_id = {$_SESSION['user_id']} AND item_id = {$page->id}");
				$sqlresult = db_do_query($page, $sql);
				if (db_num_rows($sqlresult)) {
					$subscription = db_fetch_array($sqlresult);
					$keys[] = "UNSUBSCRIBE";
					$vals[] = "hef mijn abonnement op";
				}
				
				$creator = new FormCreator();
				$inputs[] = $creator->input_select("Selecteer type abonnement", "type", $keys, $vals, "", $subscription['subscription_id']);
				
				$page->content['form'] = $creator->create("subscriptionform", $inputs);
			
			} else { // dir subscription
			
				$dir_id = $page->directory_tree[$page->dir_depth-1]['dir_id'];
			
				$page->content['title'] = "Abonnement op directory #$dir_id wijzigen";
			
				$inputs = array();
				
				$sql = db_query_select(array(array('*')), array("SUBSCRIPTION_TYPE"), 1);
				$sqlresult = db_do_query($page, $sql);
				$keys = array();
				$vals = array();
				while ($r = db_fetch_array($sqlresult)) {
					$keys[] = $r['subscription_id'];
					$vals[] = $r['name'];
				}
				
				$sql = db_query_select(array(array('*')), array("SUBSCRIBE_DIR"), "user_id = {$_SESSION['user_id']} AND dir_id = $dir_id");
				$sqlresult = db_do_query($page, $sql);
				if (db_num_rows($sqlresult)) {
					$subscription = db_fetch_array($sqlresult);
					$keys[] = "UNSUBSCRIBE";
					$vals[] = "hef mijn abonnement op";
				}
				
				$inputs[] = $creator->input_select("Selecteer type abonnement", "type", $keys, $vals, "", $subscription['subscription_id']);
				
				$page->content['form'] = $creator->create("subscriptionform", $inputs);

			}
		}
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_body($page);
		print_form($page, $page->content['form']);
	}
}

?>